{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from numpy import genfromtxt\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[      nan       nan       nan       nan       nan       nan       nan\n",
      "        nan]\n",
      " [      nan    83.      234.289   235.6     159.      107.608  1947.\n",
      "     60.323]\n",
      " [      nan    88.5     259.426   232.5     145.6     108.632  1948.\n",
      "     61.122]\n",
      " [      nan    88.2     258.054   368.2     161.6     109.773  1949.\n",
      "     60.171]\n",
      " [      nan    89.5     284.599   335.1     165.      110.929  1950.\n",
      "     61.187]\n",
      " [      nan    96.2     328.975   209.9     309.9     112.075  1951.\n",
      "     63.221]\n",
      " [      nan    98.1     346.999   193.2     359.4     113.27   1952.\n",
      "     63.639]\n",
      " [      nan    99.      365.385   187.      354.7     115.094  1953.\n",
      "     64.989]\n",
      " [      nan   100.      363.112   357.8     335.      116.219  1954.\n",
      "     63.761]\n",
      " [      nan   101.2     397.469   290.4     304.8     117.388  1955.\n",
      "     66.019]\n",
      " [      nan   104.6     419.18    282.2     285.7     118.734  1956.\n",
      "     67.857]\n",
      " [      nan   108.4     442.769   293.6     279.8     120.445  1957.\n",
      "     68.169]\n",
      " [      nan   110.8     444.546   468.1     263.7     121.95   1958.\n",
      "     66.513]\n",
      " [      nan   112.6     482.704   381.3     255.2     123.366  1959.\n",
      "     68.655]\n",
      " [      nan   114.2     502.601   393.1     251.4     125.368  1960.\n",
      "     69.564]\n",
      " [      nan   115.7     518.173   480.6     257.2     127.852  1961.\n",
      "     69.331]\n",
      " [      nan   116.9     554.894   400.7     282.7     130.081  1962.\n",
      "     70.551]]\n"
     ]
    }
   ],
   "source": [
    "data = np.genfromtxt('longley.csv',delimiter = ',')\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  234.289   235.6     159.      107.608  1947.       60.323]\n",
      " [  259.426   232.5     145.6     108.632  1948.       61.122]\n",
      " [  258.054   368.2     161.6     109.773  1949.       60.171]\n",
      " [  284.599   335.1     165.      110.929  1950.       61.187]\n",
      " [  328.975   209.9     309.9     112.075  1951.       63.221]\n",
      " [  346.999   193.2     359.4     113.27   1952.       63.639]\n",
      " [  365.385   187.      354.7     115.094  1953.       64.989]\n",
      " [  363.112   357.8     335.      116.219  1954.       63.761]\n",
      " [  397.469   290.4     304.8     117.388  1955.       66.019]\n",
      " [  419.18    282.2     285.7     118.734  1956.       67.857]\n",
      " [  442.769   293.6     279.8     120.445  1957.       68.169]\n",
      " [  444.546   468.1     263.7     121.95   1958.       66.513]\n",
      " [  482.704   381.3     255.2     123.366  1959.       68.655]\n",
      " [  502.601   393.1     251.4     125.368  1960.       69.564]\n",
      " [  518.173   480.6     257.2     127.852  1961.       69.331]\n",
      " [  554.894   400.7     282.7     130.081  1962.       70.551]]\n",
      "[[  83. ]\n",
      " [  88.5]\n",
      " [  88.2]\n",
      " [  89.5]\n",
      " [  96.2]\n",
      " [  98.1]\n",
      " [  99. ]\n",
      " [ 100. ]\n",
      " [ 101.2]\n",
      " [ 104.6]\n",
      " [ 108.4]\n",
      " [ 110.8]\n",
      " [ 112.6]\n",
      " [ 114.2]\n",
      " [ 115.7]\n",
      " [ 116.9]]\n"
     ]
    }
   ],
   "source": [
    "#切分数据\n",
    "x_data = data[1:,2:]\n",
    "y_data = data[1:,1,np.newaxis]\n",
    "\n",
    "print(x_data)\n",
    "print(y_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(16, 6)\n",
      "(16, 1)\n",
      "(16, 7)\n"
     ]
    }
   ],
   "source": [
    "print(np.mat(x_data).shape)\n",
    "print(np.mat(y_data).shape)\n",
    "\n",
    "X_data = np.concatenate((np.ones((16,1)),x_data),axis = 1)\n",
    "print(X_data.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#岭回归标准方程法实现\n",
    "def Stand_equation(Xarr,Yarr,len =0.360853388534):\n",
    "    Xmat = np.mat(Xarr)\n",
    "    Ymat = np.mat(Yarr)\n",
    "    xTx = Xmat.T*Xmat\n",
    "    xTxt = xTx + np.eye(Xarr.shape[1])*len\n",
    "    if np.linalg.eig(xTxt) == 0.0:\n",
    "        return\n",
    "    ws = xTxt.I * Xmat.T * Ymat\n",
    "    return ws"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  4.34757980e-04]\n",
      " [  2.01110267e-01]\n",
      " [  2.07407194e-02]\n",
      " [  5.53588134e-03]\n",
      " [ -1.51541245e+00]\n",
      " [  1.05675727e-01]\n",
      " [ -1.98308841e-01]]\n"
     ]
    }
   ],
   "source": [
    "w = Stand_equation(X_data,y_data)\n",
    "print(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[  83.60262973],\n",
       "        [  86.91490609],\n",
       "        [  88.10725436],\n",
       "        [  90.93040774],\n",
       "        [  96.02594097],\n",
       "        [  97.7902733 ],\n",
       "        [  98.40712205],\n",
       "        [ 100.0278164 ],\n",
       "        [ 103.25863096],\n",
       "        [ 105.05056566],\n",
       "        [ 107.44927092],\n",
       "        [ 109.49015114],\n",
       "        [ 112.85184145],\n",
       "        [ 113.96859384],\n",
       "        [ 115.33480114],\n",
       "        [ 117.68963735]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算预测值\n",
    "np.mat(X_data) * np.mat(w)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
